Skip to content

feat: add C implementation for math/base/special/minmax #6296

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 15 commits into
base: develop
Choose a base branch
from

Conversation

Neerajpathak07
Copy link
Member

Progresses #649,
Resolves #2106.

Description

What is the purpose of this pull request?

This pull request:

  • adds C implementation for math/base/special/minmax
#include <stdint.h>

double min;
double max;

stdlib_base_minmax( 4.0, -5.0, &min, &max );

Related Issues

Does this pull request have any related issues?

This pull request:

Questions

Any questions for reviewers of this pull request?

No.

Other

Any other information relevant to this pull request? This may include screenshots, references, and/or implementation notes.

No.

Checklist

Please ensure the following tasks are completed before submitting this pull request.


@stdlib-js/reviewers

@stdlib-bot stdlib-bot added the Math Issue or pull request specific to math functionality. label Mar 22, 2025
@stdlib-bot
Copy link
Contributor

stdlib-bot commented Mar 22, 2025

Coverage Report

Package Statements Branches Functions Lines
math/base/special/minmax $\color{green}253/253$
$\color{green}+100.00\%$
$\color{green}17/17$
$\color{green}+100.00\%$
$\color{green}3/3$
$\color{green}+100.00\%$
$\color{green}253/253$
$\color{green}+100.00\%$

The above coverage report was generated for the changes in this PR.

* @param info callback data
* @return Node-API value
*/
static napi_value addon( napi_env env, napi_callback_info info ) {
Copy link
Member Author

@Neerajpathak07 Neerajpathak07 Mar 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have kept the addon logic as this for now and going through https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/napi to have an idea for a utility for void functions.
for this file I have refactored this code-block.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Turns out we can convert it a very simplified fashion like:-

static napi_value addon( napi_env env, napi_callback_info info ) {
        STDLIB_NAPI_ARGV( env, info, argv, argc, 3 );
	STDLIB_NAPI_ARGV_DOUBLE( env, x, argv, 0 );
	STDLIB_NAPI_ARGV_DOUBLE( env, y, argv, 1 );
	STDLIB_NAPI_ARGV_FLOAT64ARRAY( env, z, zlen, argv, 2 );
        
        double min;
	double max;
	stdlib_base_minmax( x, y, &min, &max );

	double *op = (double *)z;
	op[ 0 ] = min;
	op[ 1 ] = max;

	return NULL;
}

just searching for an utility that can accumulate void function.

Copy link
Member Author

@Neerajpathak07 Neerajpathak07 Mar 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

something similar was also done in rempio2

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Neerajpathak07 You can do something like this:

In addon.c, you can have:

#include "stdlib/math/base/special/minmax.h"
#include "stdlib/napi/export.h"
#include "stdlib/napi/argv.h"
#include "stdlib/napi/argv_double.h"
#include "stdlib/napi/argv_float64array.h"
#include <node_api.h>

/**
* Receives JavaScript callback invocation data.
*
* @param env    environment under which the function is invoked
* @param info   callback data
* @return       Node-API value
*/
static napi_value addon( napi_env env, napi_callback_info info ) {
    STDLIB_NAPI_ARGV( env, info, argv, argc, 3 );
    STDLIB_NAPI_ARGV_DOUBLE( env, x, argv, 0 );
    STDLIB_NAPI_ARGV_DOUBLE( env, y, argv, 1 );
    STDLIB_NAPI_ARGV_FLOAT64ARRAY( env, out, outlen, argv, 2 );
    stdlib_base_minmax( x, y, &out[ 0 ], &out[ 1 ] );
    return NULL;
}

STDLIB_NAPI_MODULE_EXPORT_FCN( addon )

And, for native.js:

function minmax( x, y ) {
	var out = new Float64Array( 2 );
	addon( x, y, out );
	return [ out[ 0 ], out[ 1 ] ];
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gunjjoshi Bingo!!
I had drafted a similar logic earlier but was confused on how to go about the void function. But you just clarified it thank you soo much!!

@Neerajpathak07 Neerajpathak07 marked this pull request as ready for review March 22, 2025 18:26
@stdlib-bot stdlib-bot added the Needs Review A pull request which needs code review. label Mar 22, 2025
@Neerajpathak07
Copy link
Member Author

@gunjjoshi LMK your opinion on this!!

@Neerajpathak07
Copy link
Member Author

Not sure though why only C examples CI tests are failing for #include <node_api.h> and not for any other files.

@stdlib-bot stdlib-bot added the Potential Duplicate There might be another pull request resolving the same issue. label Apr 19, 2025
@anandkaranubc anandkaranubc self-requested a review May 11, 2025 00:34
@anandkaranubc
Copy link
Contributor

/stdlib merge

@stdlib-bot stdlib-bot added the bot: In Progress Pull request is currently awaiting automation. label May 11, 2025
@stdlib-bot
Copy link
Contributor

/stdlib merge

@anandkaranubc, the slash command failed to complete. Please check the workflow logs for details.

View workflow run

@stdlib-bot stdlib-bot removed the bot: In Progress Pull request is currently awaiting automation. label May 11, 2025
@anandkaranubc
Copy link
Contributor

/stdlib merge

@stdlib-bot stdlib-bot added bot: In Progress Pull request is currently awaiting automation. and removed bot: In Progress Pull request is currently awaiting automation. labels May 11, 2025
#include <stdlib.h>

/**
* Evaluates the min and max of two values.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Evaluates the min and max of two values.
* Evaluates the minimum and maximum values.

* @param x First value
* @param y Second value
* @param min destination to store minimum value
* @param max destination to store maximum value
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @param max destination to store maximum value
* @param x first number
* @param y second number
* @param min destination to store the minimum value
* @param max destination to store the maximum value

* @param max destination to store maximum value
*
* @example
* #include <stdint.h>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are no integers in the example. This can be removed

* double min;
* double max;
* stdlib_base_minmax( 3.14, NaN );
* // returns [ NaN, NaN ]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No return. Function type is void.

#include "stdlib/math/base/special/minmax.h"
#include "stdlib/math/base/assert/is_nan.h"
#include "stdlib/math/base/assert/is_negative_zero.h"
#include <stdlib.h>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not needed.

function minmax( x, y ) {
var out = new Float64Array( 2 );
addon( x, y, out );
return [ out[ 0 ], out[ 1 ] ];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return [ out[ 0 ], out[ 1 ] ];
return out;

*
* @example
* var v = minmax( 3.14, 4.2 );
* // returns [ 3.14, 4.2 ]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* // returns [ 3.14, 4.2 ]
* // returns <Float64Array>[ 3.14, 4.2 ]

Comment on lines 58 to 61
"@stdlib/napi/argv",
"@stdlib/napi/argv-double",
"@stdlib/napi/argv-float64array",
"@stdlib/napi/export",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are not needed for examples/benchmark.


v = minmax( NaN, 3.14 );
t.strictEqual( isnan( v[ 0 ] ), true, 'returns NaN' );
t.strictEqual( isnan( v[ 1 ] ), true, 'returns NaN' );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
t.strictEqual( isnan( v[ 1 ] ), true, 'returns NaN' );
t.strictEqual( isnan( v[ 0 ] ), true, 'returns expected value' );

Applies here and below.

#endif

/**
* Returns the minimum and maximum values.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Returns the minimum and maximum values.
* Evaluates the minimum and maximum values.

The function doesn't return anything.

---
type: pre_commit_static_analysis_report
description: Results of running static analysis checks when committing changes.
report:
  - task: lint_filenames
    status: passed
  - task: lint_editorconfig
    status: passed
  - task: lint_markdown
    status: na
  - task: lint_package_json
    status: na
  - task: lint_repl_help
    status: na
  - task: lint_javascript_src
    status: passed
  - task: lint_javascript_cli
    status: na
  - task: lint_javascript_examples
    status: na
  - task: lint_javascript_tests
    status: passed
  - task: lint_javascript_benchmarks
    status: na
  - task: lint_python
    status: na
  - task: lint_r
    status: na
  - task: lint_c_src
    status: passed
  - task: lint_c_examples
    status: na
  - task: lint_c_benchmarks
    status: na
  - task: lint_c_tests_fixtures
    status: na
  - task: lint_shell
    status: na
  - task: lint_typescript_declarations
    status: na
  - task: lint_typescript_tests
    status: na
  - task: lint_license_headers
    status: passed
---
@anandkaranubc anandkaranubc self-requested a review May 11, 2025 06:29
---
type: pre_commit_static_analysis_report
description: Results of running static analysis checks when committing changes.
report:
  - task: lint_filenames
    status: passed
  - task: lint_editorconfig
    status: passed
  - task: lint_markdown
    status: na
  - task: lint_package_json
    status: na
  - task: lint_repl_help
    status: na
  - task: lint_javascript_src
    status: na
  - task: lint_javascript_cli
    status: na
  - task: lint_javascript_examples
    status: na
  - task: lint_javascript_tests
    status: na
  - task: lint_javascript_benchmarks
    status: na
  - task: lint_python
    status: na
  - task: lint_r
    status: na
  - task: lint_c_src
    status: na
  - task: lint_c_examples
    status: na
  - task: lint_c_benchmarks
    status: passed
  - task: lint_c_tests_fixtures
    status: na
  - task: lint_shell
    status: na
  - task: lint_typescript_declarations
    status: na
  - task: lint_typescript_tests
    status: na
  - task: lint_license_headers
    status: passed
---

for ( i = 0; i < 10; i++ ) {
stdlib_base_minmax( x[ i ], y[ i ], &min, &max );
printf( "x: %lf => min: %lf, y: %lf, minmax(x, y): %lf\n", x[ i ], y[ i ], min, max );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
printf( "x: %lf => min: %lf, y: %lf, minmax(x, y): %lf\n", x[ i ], y[ i ], min, max );
printf( "x: %lf, y: %lf => min: %lf, max: %lf\n", x[ i ], y[ i ], min, max );

---
type: pre_commit_static_analysis_report
description: Results of running static analysis checks when committing changes.
report:
  - task: lint_filenames
    status: passed
  - task: lint_editorconfig
    status: passed
  - task: lint_markdown
    status: passed
  - task: lint_package_json
    status: na
  - task: lint_repl_help
    status: na
  - task: lint_javascript_src
    status: na
  - task: lint_javascript_cli
    status: na
  - task: lint_javascript_examples
    status: na
  - task: lint_javascript_tests
    status: na
  - task: lint_javascript_benchmarks
    status: passed
  - task: lint_python
    status: na
  - task: lint_r
    status: na
  - task: lint_c_src
    status: passed
  - task: lint_c_examples
    status: passed
  - task: lint_c_benchmarks
    status: na
  - task: lint_c_tests_fixtures
    status: na
  - task: lint_shell
    status: na
  - task: lint_typescript_declarations
    status: na
  - task: lint_typescript_tests
    status: na
  - task: lint_license_headers
    status: passed
---
#include "stdlib/math/base/assert/is_negative_zero.h"

/**
* Evaluates the minimum and maximum values in a single pass.
Copy link
Contributor

@anandkaranubc anandkaranubc May 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the reviewers: I've updated the description, but if "Evaluates the minimum and maximum values." is sufficiently clear on its own, I will revert the changes.

anandkaranubc
anandkaranubc previously approved these changes May 11, 2025
Signed-off-by: Karan Anand <[email protected]>
anandkaranubc
anandkaranubc previously approved these changes May 11, 2025
Signed-off-by: Karan Anand <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Math Issue or pull request specific to math functionality. Needs Review A pull request which needs code review. Potential Duplicate There might be another pull request resolving the same issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[RFC]: Add C implementation for @stdlib/math/base/special/minmax
4 participants